import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../utils/EventMgr.dart';
import '../../../utils/logger.dart';

/// 售后控制器
class AfterSalesOrderListController extends BaseController with SingleGetTickerProviderMixin {
  late TabController StatusTabController;

  late TabController baojiaTabController;
  final ScrollController scrollController = ScrollController();

  int page = 1;

  int type = 7;

  int initTabIndex = 0;

  String keyowrds = '';

  bool hasNextPage = false;

  ///订单列表
  List orderList = [];

  ///保价统计
  Map total = {};

  ///保价列表
  List baojiaList = [];

  ///保价记录列表
  List recordList = [];

  @override
  void onInit() {
    ///监听路由
    EventMgr.on(EventType.refreshOrderList, (arg) {
      logger.d('售后订单列表刷新>>>>>>>');
      getMyorderLists();
    });

    // baojiaTotal();
    // getbaojiaList();
    getMyorderLists();
    StatusTabController = TabController(length: 2, vsync: this);
    StatusTabController.addListener(() {
      if (StatusTabController.index.toDouble() == StatusTabController.animation?.value) {
        if (StatusTabController.index == 0) {
          type = 7;
        } else {
          type = 8;
        }
        page = 1;
        getMyorderLists();
      }
    });
    //---------------------------

    //----保价控制器----
    baojiaTabController = TabController(length: 2, vsync: this);
    baojiaTabController.addListener(() {
      if (baojiaTabController.index.toDouble() == baojiaTabController.animation?.value) {
        // print("切换了状态${baojiaTabController.index}");
        if (baojiaTabController.index == 0) {
          getbaojiaList();
        } else {
          getRecord();
        }
      }
    });
    WidgetsBinding.instance.addPostFrameCallback((_) {
      scrollController.jumpTo(scrollController.position.maxScrollExtent);
    });
    super.onInit();
  }

  //获取我的订单列表
  getMyorderLists() {
    Tools.showLoading();
    Network.getMyOrderList(type, page, 5).then((value) {
      print("类型$type,数据：${value}");
      if (page == 1) {
        orderList = value['list'];
      } else {
        orderList.addAll(value['list']);
      }
      hasNextPage = value['hasNextPage'];
      update();
      Tools.hideLoading();
    }).catchError((error) {
      Tools.hideLoading();
    });
  }

  ///保价顶部统计
  void baojiaTotal() {
    Tools.showLoading();
    Network.baojiadingbuchaxun().then((value) {
      total = value;
      update();
      Tools.hideLoading();
    }).catchError((error) {
      Tools.hideLoading();
    });
  }

  ///保价列表
  void getbaojiaList() {
    Tools.showLoading();
    Network.baojialiebiao({
      "pageNum": 1,
      "pageSize": 100,
      "keyowrds": '',
    }).then((value) {
      print("这里是保价列表:$value");
      baojiaList = value['list'];
      update();
      Tools.hideLoading();
    }).catchError((error) {
      Tools.hideLoading();
    });
  }

  ///保价记录
  void getRecord() {
    Tools.showLoading();
    Network.baojiashenqingjilu({"pageNum": 1, "pageSize": 100, "keyowrds": ''}).then((value) {
      print(value);
      recordList = value['list'];
      update();
      Tools.hideLoading();
    }).catchError((error) {
      Tools.hideLoading();
    });
  }

  ///申请保价
  void apply(String goodsId, String orderId, String sn) {
    Tools.showLoading();
    Network.shenqingbaojia({"goodsId": goodsId, "orderId": orderId, "sn": sn}).then((value) {
      print(value);

      update();
      Tools.hideLoading();
    }).catchError((error) {
      Tools.hideLoading();
    });
  }

  @override
  void onPageRefresh() async {
    super.onPageRefresh();
    page = 1;
    getMyorderLists();
    baseRefreshCtr.refreshCompleted(resetFooterState: true);
    baseRefreshCtr.refreshCompleted(resetFooterState: true);
  }

  @override
  void onPageLoading() async {
    if (hasNextPage == true) {
      super.onPageLoading();
      page += 1;
      getMyorderLists();
      baseRefreshCtr.loadComplete();
      baseRefreshCtr.loadComplete();
    }
  }

  @override
  void onClose() {
    StatusTabController.dispose();
    baojiaTabController.dispose();
    EventMgr.off(EventType.refreshOrderList);

    super.onClose();
  }
}
